/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.opu.db_vdumper.ui.component.tabel.views;

import java.awt.Point;

/**
 *  Performs mathematical calculations to draw
 * @author Potochnak Yana
 */
public class Geometry {

    /**
     *  The equation of a line passing through two points<pre>
     *   x - x1    y - y1
     *  ------- = -------
     *  x2 - x1 = y2 - y1
     * </pre>
     * On the plane you see above equation of a line can be simplified: <pre>
     *           y2 - y1
     *  y = y1 + ------- (x - x1)
     *           x2 - x1
     * 
     *           y2 - y1
     *  Fraction ------- = k is called the slope of the line.
     *           x2 - x1
     * </pre>
     * @param p1 first point
     * @param p2 second point
     * @param x X position on line
     * @return Y position on line
     * @see #lineSlope(java.awt.Point, java.awt.Point) 
     */
    public static int linePassingTwoPoints(Point p1, Point p2, int x){
        return p1.y + (lineSlope(p2, p1)) * (x - p1.x);
    }
    
    /**
     *  Same that {@link #linePassingTwoPoints(java.awt.Point, java.awt.Point, int)},
     * only for calculating x
     * @param p1 first point
     * @param p2 second point
     * @param y Y position on line
     * @return X position on line
     */
    public static int linePassingTwoPointsForY(Point p1, Point p2, int y){
        return p1.x + (lineSlope(p2, p1)) * (y - p1.y);
    }

    /**
     * <pre>
     *           y2 - y1
     *  Fraction ------- = k is called the slope of the line.
     *           x2 - x1
     * </pre>
     * @param p2 first point
     * @param p1 second point
     * @return the slope of the line in equation of a line passing
     *      through two points.
     */
    public static int lineSlope(Point p2, Point p1) {
        return (p2.y - p1.y) / (p2.x - p1.x);
    }
}
